package model;

import java.util.ArrayList;
import org.jbox2d.common.Vec2;
import org.jbox2d.dynamics.Body;
import org.newdawn.slick.Image;
import behaviors.Behavior;

public abstract class MetaBody {

	public static int UNDEFINED = -1, CIRCLE = 0, POLYGON = 1, COMPOUND = 2;

	public float x, y, modX, modY, sizeX, sizeY, radius, scale, restitution,
			density, friction, angularDamping, linearDamping, I, angle;

	public Vec2 vertices[];
	public String name;
	public Image image;
	public int type;
	public Body body;
	public long timeToDie;
	public long health;

	public boolean facing, orient, isBullet, isJumping, isOrientable,
			underWater, recycle, isSensor, fixedRotation;

	public ArrayList<Joints> jointList;
	public ArrayList<Entity> entityList;
	public ArrayList<Behavior> behaviorList;

	// clone this object
	@Override
	public Entity clone() {
		Entity obj = new Entity();
		
		obj.health = this.health;
		obj.fixedRotation = this.fixedRotation;
		obj.isSensor = this.isSensor;
		obj.recycle = this.recycle;
		obj.underWater = this.underWater;
		obj.angle = this.angle;
		obj.isOrientable = this.isOrientable;
		obj.I = this.I;
		obj.linearDamping = this.linearDamping;
		obj.angularDamping = this.angularDamping;
		obj.timeToDie = this.timeToDie;
		obj.isJumping = this.isJumping;
		obj.isBullet = this.isBullet;
		obj.orient = this.orient;
		obj.facing = this.facing;
		obj.type = this.type;
		obj.name = this.name;
		obj.image = this.image;
		obj.radius = this.radius;
		obj.sizeX = this.sizeX;
		obj.sizeY = this.sizeY;
		obj.x = this.x;
		obj.y = this.y;
		obj.modX = this.modX;
		obj.modY = this.modY;
		obj.scale = this.scale;
		obj.vertices = this.vertices;
		obj.density = this.density;
		obj.friction = this.friction;
		obj.restitution = this.restitution;
		obj.body = this.body;
		obj.entityList = this.entityList;
		obj.jointList = this.jointList;

		// clone the behavior list if needed
		obj.behaviorList = new ArrayList<Behavior>();
		for (Behavior b : this.behaviorList) {
			obj.behaviorList.add((Behavior) b.clone());
		}

		// clone compound objects
		if (this.type == Entity.COMPOUND && this.entityList != null) {
			obj.entityList = new ArrayList<Entity>();
			for (Entity e : this.entityList) {
				obj.entityList.add(e.clone());
			}
		}
		if (this.type == Entity.COMPOUND && this.jointList != null) {
			obj.jointList = new ArrayList<Joints>();
			for (Joints j : this.jointList) {
				obj.jointList.add((Joints) j.clone());
			}
		}
		return obj;
	}

	// set some defaults
	public void setDefault() {
		this.health = 100;
		this.fixedRotation = false;
		this.isSensor = false;
		this.recycle = false;
		this.underWater = false;
		this.angle = 0;
		this.isOrientable = false;
		this.isJumping = false;
		this.timeToDie = -1;
		this.orient = false;
		this.jointList = new ArrayList<Joints>();
		this.entityList = new ArrayList<Entity>();
		this.behaviorList = new ArrayList<Behavior>();
		this.facing = false;
		this.image = null;
		this.isBullet = false;
		this.body = null;
		this.modX = 0;
		this.modY = 0;
		this.I = 0;
		this.linearDamping = 0;
		this.angularDamping = 0;
		this.type = UNDEFINED;
		this.name = "UNDEFINED";
		this.radius = 0;
		this.sizeX = 0;
		this.sizeY = 0;
		this.x = 0;
		this.y = 0;
		this.scale = 0;
		this.vertices = null;
		this.density = 1;
		this.friction = 0.5f;
		this.restitution = 0.5f;
	}

	public void setPosition(Vec2 v) {
		this.x = v.x;
		this.y = v.y;

		if (entityList != null) {
			for (Entity e : entityList) {
				e.x = v.x + e.modX;
				e.y = v.y + e.modY;
			}
		}
	}
}
